package com.oocl.crm.db;
public class DoubleLinkList 
{
	//1.初始化
	public Node first = null;
	public Node last = null;
	public int size=0; 
	
	//2.插入节点: insertFirst, insertLast, insertAfter(target, value)
	public void insertFirst(Node node)
	{
		if(first == null){  
			last = node;  
		}else{  
			node.right = first;  
			first.left = node;  
		}  
		first = node;  
		size++;
	}
	
	public void insertLast(Node node)
	{
		if(first == null){
			first = node;
		}else{
			last.right = node;
			node.left = last;
		}
		last = node;
		size++;
	}
	
	//3.删除节点: deleteFirst, deleteLast, delete(Customer customer)
	public Node deleteFirst() throws Exception
	{
		if(first == null)
			throw new Exception("empty!");
		Node temp = first;
		if(first.right == null)
		{
			first = null;
			last = null;
		}
		else
		{
			first.right.left = null;
			first = first.right;
		}
		size--;
		return temp;
	}
	
	public Node deleteLast() throws Exception{
		if(first == null)
			throw new Exception("empty!");
		Node temp = last;
		if(first.right == null)
		{
			first = null;
			last = null;
		}
		else
		{
			last.left.right = null;
			last = last.left;
		}
		size--;
		return temp;
	}
	
	public Node getNode(int index)
    {
    	if (index < 0 || index >= size)
      		throw new IndexOutOfBoundsException();
   		
    	Node node=first;
   		for (int i = 1; i < index; i++)
     		node = node.right;
    	return node;
  	}
  	
	public int size()
  	{
    	return size;
  	}

	
	
}


